package kr.jin.zeroprojectone;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class InsertSchedule extends Activity {
	static ArrayList<String> textList1 = new ArrayList<String>();//시작시간리스트
	static ArrayList<String> textList2 = new ArrayList<String>();//종료시간리스트
	static ArrayList<String> textList3 = new ArrayList<String>();//활동리스트
	StringBuilder sb1;
	StringBuilder sb2;
	StringBuilder sb3;
	StringBuilder sb33;
	int mYear, mMonth, mDay, mHour1, mHour2, mMinute1, mMinute2;
	EditText startTime;
	EditText stopTime;
	EditText mActivity;
	TextView mInsertText1;//시작시간 텍스트
	TextView mInsertText2;//종료시간 텍스트
	TextView mInsertText3;//활동 텍스트
	//입력창에서 가져온 문자
	String text1;
	String text2;
	String text3;
	@Override
	protected void onCreate(Bundle savedInstanceState){ 	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.insertschedule);
		
		Calendar cal = new GregorianCalendar();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH);
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		mHour1 = cal.get(Calendar.HOUR_OF_DAY);
		mMinute1 = cal.get(Calendar.MINUTE);
		mHour2 = cal.get(Calendar.HOUR_OF_DAY);
		mMinute2 = cal.get(Calendar.MINUTE);
		
		startTime = (EditText) findViewById(R.id.insertschedule_start_edit);
		stopTime = (EditText) findViewById(R.id.insertschedule_stop_edit);
		mActivity = (EditText) findViewById(R.id.inserstchedule_activiry_edit);
		mInsertText3 = (TextView) findViewById(R.id.insertschedule_insert_text3);
		mInsertText2 = (TextView) findViewById(R.id.insertschedule_insert_text2);
		mInsertText1 = (TextView) findViewById(R.id.insertschedule_insert_text1);
		setTextView();
	}
	private void setTextView() {
		SharedPreferences pref = getSharedPreferences("schedule", 0);
		//활동텍스트에 저장값 뿌려주기
		textList3.clear();
		int size3 = pref.getInt("lastSize3", 0);
		for(int i=0;i<size3;i++){
			textList3.add(pref.getString("last_status3"+i, null));
		}
		sb3 = new StringBuilder();
		//sb3.append("*저장된 스케쥴*\n");
		for(int i = 0; i<textList3.size();i++){
			sb3.append(textList3.get(i));				
		}
		sb3.append("---\n");
		mInsertText3.setText(sb3);	
		//종료시간텍스트에 저장값 뿌려주기
		textList2.clear();
		int size2 = pref.getInt("lastSize2", 0);
		for(int i=0;i<size2;i++){
			textList2.add(pref.getString("last_status2"+i, null));
		}
		sb2 = new StringBuilder();
		//sb2.append("시작시간\n");
		for(int i = 0; i<textList2.size();i++){
			sb2.append(textList2.get(i));				
		}
		sb2.append("---\n");
		mInsertText2.setText(sb2);
		//시작시간텍스트에 저장값 뿌려주기
		textList1.clear();
		int size1 = pref.getInt("lastSize1", 0);
		for(int i=0;i<size1;i++){
			textList1.add(pref.getString("last_status1"+i, null));
		}
		sb1 = new StringBuilder();
		//sb1.append("종료시간\n");
		for(int i = 0; i<textList1.size();i++){
			sb1.append(textList1.get(i));				
		}
		sb1.append("---\n");
		mInsertText1.setText(sb1);
		
	}
	public void mOnClick(View v){
		String toast1 = getResources().getString(R.string.class_insertschedule_toast1);
		String saveText = getResources().getString(R.string.class_insertschedule_save_text);
		String toast2 = getResources().getString(R.string.class_insertschedule_toast2);
		String saveConfirm = getResources().getString(R.string.class_insertschedule_save_confirm);
		
		switch(v.getId()){
		case R.id.insertschedule_start_btn:
			new TimePickerDialog(this, mTimeSetListener1, mHour1, mMinute1, false).show();
			break;
		case R.id.insertschedule_stop_btn:
			new TimePickerDialog(this, mTimeSetListener2, mHour2, mMinute2, false).show();
			break;
		case R.id.insertschedule_insert1_btn:
			String text3 = mActivity.getText().toString();
			String text2 = stopTime.getText().toString();
			String text1 = startTime.getText().toString();
			if(text1.equals("")|text2.equals("")|text3.equals("")){
				Toast.makeText(this, toast1, Toast.LENGTH_LONG).show();
			}else{
				//입력값을 활동텍스트에 출력
				String str3 = text3 +"\n";
				textList3.add(str3);				
				sb3 = new StringBuilder();
				for(int i = 0; i<textList3.size();i++){
					sb3.append(textList3.get(i));				
				}
				sb3.append("\n<"+saveText+">");
				mInsertText3.setText(sb3);
				
				//입력값을 종료시간텍스트에 출력
				String str2 = text2 +"\n";
				textList2.add(str2);				
				sb2 = new StringBuilder();
				for(int i = 0; i<textList2.size();i++){
					sb2.append(textList2.get(i));				
				}
				mInsertText2.setText(sb2);
				//입력값을 종료시간텍스트에 출력
				String str1 = text1 +"\n";
				textList1.add(str1);				
				sb1 = new StringBuilder();
				for(int i = 0; i<textList1.size();i++){
					sb1.append(textList1.get(i));				
				}
				
				mInsertText1.setText(sb1);		
				
			}
			
			break;
		case R.id.insertschedule_delete_btn:
			try{
				//활동텍스트에서 한줄 삭제
				textList3.remove(textList3.size()-1);
				sb3.setLength(0);
				for(int i = 0; i<textList3.size();i++){
					sb3.append(textList3.get(i));			
				}
				sb3.append("\n<"+saveText+">");
				mInsertText3.setText(sb3);
				//종료시간 텍스트에서 한줄 삭제
				textList2.remove(textList2.size()-1);
				sb2.setLength(0);
				for(int i = 0; i<textList2.size();i++){
					sb2.append(textList2.get(i));			
				}
				mInsertText2.setText(sb2);
				//시작시간 텍스트에서 삭제
				textList1.remove(textList1.size()-1);
				sb1.setLength(0);
				for(int i = 0; i<textList1.size();i++){
					sb1.append(textList1.get(i));			
				}
				mInsertText1.setText(sb1);
			}catch(Exception e){
				Toast.makeText(this, toast2, Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.insertschedule_save_btn:
			SharedPreferences pref = getSharedPreferences("schedule", 0);
			SharedPreferences.Editor editor = pref.edit();
			//활동리스트값 저장
			editor.putInt("lastSize3",textList3.size());
			for(int i = 0; i<textList3.size(); i++){
				editor.remove("last_status3" + i);
				editor.putString("last_status3" + i, textList3.get(i));
			}
			//활동텍스트에 다시 출력
			/*textList3.clear();
			int size33 = pref.getInt("lastSize3", 0);
			for(int i=0;i<size33;i++){
				textList3.add(pref.getString("last_status3"+i, null));
			}
			sb33 = new StringBuilder();
			sb33.delete(0, size33);
			for(int i = 0; i<textList3.size();i++){
				sb33.append(textList3.get(i));		
				mInsertText3.setText(sb33);
			}*/
			
			//종료시간리스트값저장
			editor.putInt("lastSize2",textList2.size());
			for(int i = 0; i<textList2.size(); i++){
				editor.remove("last_status2" + i);
				editor.putString("last_status2" + i, textList2.get(i));
			}
			
			//시작시간리스트값저장
			editor.putInt("lastSize1",textList1.size());
			for(int i = 0; i<textList1.size(); i++){
				editor.remove("last_status1" + i);
				editor.putString("last_status1" + i, textList1.get(i));
			}
			editor.commit();
			Toast.makeText(this, saveConfirm, Toast.LENGTH_LONG).show();
			setTextView();
			break;
		
		case R.id.insertschedule_insert2_btn:
			/*SharedPreferences pref1 = getSharedPreferences("schedule", 0);
			
			textList3.clear();
			int size3 = pref1.getInt("lastSize3", 0);
			for(int i=0;i<size3;i++){
				textList3.add(pref1.getString("last_status3"+i, null));
			}
			
			textList2.clear();
			int size2 = pref1.getInt("lastSize2", 0);
			for(int i=0;i<size2;i++){
				textList2.add(pref1.getString("last_status2"+i, null));
			}
					
			textList1.clear();
			int size1 = pref1.getInt("lastSize1", 0);
			for(int i=0;i<size1;i++){
				textList1.add(pref1.getString("last_status1"+i, null));
			}*/
			
			
			
			//4단계 측정페이지 화면에 뿌려주기
			/*Intent intent = new Intent();	
			intent.putStringArrayListExtra("textList1", textList1);
			intent.putStringArrayListExtra("textList2", textList2);
			intent.putStringArrayListExtra("textList3", textList3);
			setResult(RESULT_OK,intent);	*/	
			finish();
				
			break;
		}
	}
	TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			view.is24HourView();
			mHour1 = hourOfDay;
			mMinute1  = minute;		
			UpdateNow(1);
		}
	};
	TimePickerDialog.OnTimeSetListener mTimeSetListener2 = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			view.is24HourView();
			mHour2 = hourOfDay;
			mMinute2  = minute;
			UpdateNow(2);
		}
	};
	/*DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			
		}
	};*/
	void UpdateNow(int num){
		switch(num){
		case 1:
			startTime.setText(String.format("%02d:%02d", mHour1, mMinute1));
			break;
		case 2:
			stopTime.setText(String.format("%02d:%02d", mHour2, mMinute2));
			break;
		}
	
	
	}
		
	
}
